//index.js
import Notify from '../../ui/vant-weapp/dist/notify/notify'
//获取应用实例
const app = getApp()

Page({
  data: {
    motto: '芦苇就是傻B 哈哈',
    message: 'hello world!',
    messageFlag: true,
    tapIndex: 1,
    userInfo: {},
    hasUserInfo: false,
    popupFlag: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    dateTime: {
      minHour: 10,
      maxHour: 20,
      minDate: new Date().getTime(),
      maxDate: new Date(2029, 12, 31).getTime(),
      currentDate: new Date().getTime()
    },
    activeNames: ['1'],
    counterInfo: {
      input: ' ',
      maxStrLength: 36
    }
  },
  onCounterBtnChange(event) {
    let btnInfo = event.currentTarget.dataset.info

    if ('C' === btnInfo) {
      this.setData({
        'counterInfo.input': ' '
      })
    } else if ('=' === btnInfo) {

    } else {
      if (!this.checkBtnInfo(btnInfo)) {
        return;
      }
      let oldInput = this.data.counterInfo.input
      this.setData({
        'counterInfo.input': oldInput + btnInfo
      })
    }
  },
  checkBtnInfo(btnInfo) {
    let retFlag = true
    //字符串长度
    if (this.data.counterInfo.input.length >= this.data.counterInfo.maxStrLength) {
      this.showNotify('字符串长度最长为' + this.data.counterInfo.maxStrLength)
      retFlag = false
      return retFlag
    }

    return retFlag
  },
  showNotify(info, duration) {
    info || (info = '通知')
    duration || (duration  = 1000)

    Notify({
      text: info,
      duration: duration
    });
  },
  onCollapsChange(event) {
    this.setData({
      activeNames: event.detail
    });
  },
  onDateTimeChange: function(event) {
    this.setData({
      'dateTime.currentDate': 111
    });
    console.log(event.detail);
  },
  onDateTimeCancel: function(event) {
    this.setData({
      popupFlag: false
    })
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onPopupTestClick: function() {
    this.setData({
      popupFlag: true
    })
  },
  onPopupClose: function() {
    this.setData({
      popupFlag: false
    })
  },
  tapTest: function() {
    //this.tapIndex++;
    console.log('ttt')
    wx.showToast({
      title: '加载中...',
      icon: 'loading',
      duration: 3000
    })
    wx.showModal({
      title: 'title',
      content: 'info',
    })
  },
  onLoad: function() {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
})